import {
  Controller,
  Get,
  Post,
  Response,
  Request,
  Render,
  Body,
  Query,
  Param,
} from '@nestjs/common'; //装饰器
import { UserService } from './user.service';
import { ApiTags, ApiOperation, ApiProperty } from '@nestjs/swagger';
import { UserModel } from './user.model';

// 数据传输对象,接口字段类型
class AddUserData {
  @ApiProperty({
    description: '用户姓名',
    example: '大毛',
  })
  username: string;
  @ApiProperty({
    description: '年龄',
    example: 18,
  })
  age: number;
}

@Controller('user')
@ApiTags('用户中心')
export class UserController {
  constructor(readonly userService: UserService) {}
  @Get()
  // @Render('index')
  async index() {
    // console.log('query: ', query);
    // res.cookie('name', 'coderqiqin', { maxAge: 3600, httpOnly: true });
    // return this.userService.index(query.id);
    console.log('await UserModel.find(): ', await UserModel.find());
    return {
      message: 'nestjs 模版传值',
      list: await UserModel.find(),
    };
  }

  @Get('a*a/:id')
  @ApiOperation({
    summary: '模糊匹配',
  })
  anyget(@Param('id') id: string) {
    console.log('id: ', id);
    return '模糊匹配';
  }

  @Get(':id')
  one(@Response() res, @Param() param) {
    console.log('param: ', param);
    res.json({
      name: param.id,
      age: 22,
    });
  }

  @Post('add')
  @ApiOperation({
    summary: '添加用户',
  })
  async addUser(@Body() body: AddUserData) {
    console.log('body: ', body);
    // console.log('cookie', req.cookies.name);
    return await UserModel.create({
      username: 'asdas1',
      age: '撒旦法大赛',
    });
    return { msg: '添加用户成功' };
    // res.redirect('/user');
  }
}
